//
//  RWMessageViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/28.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWMessageViewController.h"
#import "RWMessageModel.h"
#import "RWDetailMessageViewController.h"

@interface RWMessageViewController (){

    NSArray *images;
    NSMutableArray *titiles;
    NSMutableArray *detailTitles;
    NSMutableArray *messages;    //内部存储数组（系统消息、订单通知、订阅通知、公告）
    NSString *nextPageTitle;
    NSArray *detailMessage;
}

@property (nonatomic, copy) NSArray *messageModels;
@property (nonatomic, copy) RWMessageModel *messageModel;


@end

@implementation RWMessageViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self loadDatas];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
    
    _messageModels = [[NSArray alloc] init];
    _messageModel = [[RWMessageModel alloc] init];
    detailMessage = [NSMutableArray array];
    images = @[@"set_bj_icon", @"order_bj_icon", @"read_bj_icon", @"notice_bj_icon"];
    titiles = [NSMutableArray arrayWithObjects:@"系统消息", @"订单通知", @"订阅通知", @"公告", nil];
    detailTitles = [NSMutableArray arrayWithObjects:@"无", @"无", @"无", @"无", nil];
}

- (void)loadDatas{
    
    FTYUser *user = [FTYUser sharedUser];
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=membermsg&member_id=%@&token=%@", user.userID, user.userToken] parameters:nil block:^(id result, NSError *error) {
        
        if (!error) {
            
            DLog(@"消息请求成功：%@", result);
            NSDictionary *err = [result objectForKey:@"err"];
            if (![err[@"errorcode"] isEqualToString:@"500"]) {
                
                NSArray *info = [result objectForKey:@"info"];
                _messageModels = [RWMessageModel mj_objectArrayWithKeyValuesArray:info];
                
                for (int i=0; i<_messageModels.count; i++) {
                    
                    RWMessageModel *model = _messageModels[i];
                    [titiles addObject:model.name];
                    [messages addObject:model.data];
                }
                
                for (int i=0; i<messages.count; i++) {
                    
                    NSString *title = [messages[i][0] objectForKey:@"message_title"];
                    if (!title) {
                        title = @"无";
                    }
                    [detailTitles addObject:title];
                }
            }
            
            [self setNavigation];
            [self setMessageUI];
        } else {
            DLog(@"消息请求失败：%@", error);
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"消息中心";
}

- (void)setMessageUI{
   
    _messageTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_messageTV];
    _messageTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _messageTV.dataSource = self;
    _messageTV.delegate = self;
}

#pragma mark - Config Invoke Methods

#pragma mark - Notificaiton Method

#pragma mark - Notificaiton Invoke Methods

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return titiles.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 80.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * const messageID = @"messageID";
    UITableViewCell *messageCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:messageID];
    
    NSInteger row = indexPath.row;
    
    messageCell.imageView.image = [UIImage imageNamed:images[row]];
    messageCell.textLabel.text = titiles[row];
    messageCell.textLabel.font = FTY_FONTTSIZE(13);
    messageCell.detailTextLabel.text = detailTitles[row];
    messageCell.detailTextLabel.font = FTY_FONTTSIZE(13);
    messageCell.accessoryView.backgroundColor = [FTYColor redColor];
    
//    UILabel *timeLab = [[UILabel alloc] init];
//    [messageCell addSubview:timeLab];
//    timeLab.text = @"2019年20月39日 90:80";
//    timeLab.textColor = [FTYColor grayColor];
//    timeLab.font = FTY_FONTTSIZE(12);
//    [timeLab sizeToFit];
//    [timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_offset(10.*FTY_HEIGHT_BASE);
//        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
//    }];
    
    //底部线
    UIView *line = [[UIView alloc] init];
    [messageCell addSubview:line];
    line.backgroundColor = [FTYColor grayColor];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_offset(0);
        make.height.mas_offset(.5*FTY_HEIGHT_BASE);
    }];
    
    return messageCell;
}

#pragma mark - <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSInteger row = indexPath.row;
    nextPageTitle = titiles[row];
    detailMessage = messages[row];
    [self gotoSystemMessage];
}

#pragma mark - Private Invoke Methods

- (void)gotoSystemMessage{

    RWDetailMessageViewController *detailMessageVC = [[RWDetailMessageViewController alloc] init];
    detailMessageVC.detailMessage = detailMessage;
    detailMessageVC.title = nextPageTitle;
    [self.navigationController pushViewController:detailMessageVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
